<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace form\element\form;

use form\element\form\traits\Append;
use form\element\form\traits\Prepend;
use form\element\form\traits\Value;
use form\element\traits\Classes;
use form\element\traits\Css;
use form\element\traits\Id;
use form\element\traits\Js;

/**
 * 下拉
 */
class Select extends Column
{
    use Append, Prepend,Classes, Id, Css, Js, Value;

    /**
     * options
     * @var array
     */
    protected $options = [];

    /**
     * 选中的元素
     * @var
     */
    protected $selecteValues;

    /**
     * 是否多选
     * @var bool
     */
    protected $multiple = false;

    /**
     * 设置select选项
     * @param $options
     * @param string|null $selected
     * @return $this
     */
    public function options($options, ?string $selected)
    {
        $this->options = $options;
        $this->selecteValues = $selected;
        return $this;
    }

    public function multiple(bool $m)
    {
        $this->multiple = $m;
        return $this;
    }

    protected function getMuliple(): string
    {
        return $this->multiple ? 'multiple' : '';
    }

    /**
     * @inheritDoc
     */
    protected function normal(): string
    {
        $class = $this->getClass();
        $label = $this->getLabel();
        $id = $this->getId();
        $multiple = $this->getMuliple();
        $disabled = $this->getDisabled();
        $readonly = $this->getReadonly();
        $tips = $this->getTips();
        $append = $this->getAppend();
        $prepend = $this->getPrepend();
        $labelClass = $this->getLabelClass();
        $option = '';
        if (empty($this->options)) {
            $option .= '<option value="0" >--请选择--</option>' . PHP_EOL;
        } else {
            foreach ($this->options as $key => $value) {
                $selected = '';
                if ($key === $this->selecteValues) {
                    $selected = ' selected="selected"';
                }
                $option .= '<option value="' . $key . '" ' . $selected . ' >' . $value . '</option>' . PHP_EOL;
            }
        }

        if (empty($append) && empty($prepend)) {
            $select = <<<HTML
<select name="$this->name" id="$id" $multiple class="form-control" $disabled $readonly >
        $option
    </select>
HTML;
        } else {
            $select = <<<HTML
<div class="input-group">
$prepend
<select name="$this->name" id="$id" $multiple class="form-control" $disabled $readonly >
        $option
    </select>
    $append
</div>
HTML;

        }

        return <<<HTML
<div class="$class">
    <label for="$id" class="$labelClass">$label</label>
    $select
    $tips
</div>
HTML;

    }

    /**
     * @inheritDoc
     */
    protected function group()
    {
        return $this->normal();
    }

    /**
     * @inheritDoc
     */
    protected function grid()
    {
        return $this->normal();
    }

    /**
     * @inheritDoc
     */
    protected function table()
    {
        return $this->normal();
    }

    /**
     * @inheritDoc
     */
    protected function vertical()
    {
        return $this->normal();
    }

    /**
     * @inheritDoc
     */
    protected function inline()
    {
        return $this->normal();
    }

    public function generate()
    {
        $class = $this->getClass();
        $label = $this->getLabel();
        $id = $this->getId();
        $multiple = $this->getMuliple();
        $disabled = $this->getDisabled();
        $readonly = $this->getReadonly();
        $tips = $this->getTips();
        $append = $this->getAppend();
        $prepend = $this->getPrepend();

        $option = '';
        if (empty($this->options)) {
            $option .= '<option value="0" >--请选择--</option>' . PHP_EOL;
        } else {
            foreach ($this->options as $key => $value) {
                $selected = '';
                if ($key === $this->selecteValues) {
                    $selected = ' selected="selected"';
                }
                $option .= '    <option value="' . $key . '" ' . $selected . ' >' . $value . '</option>' . PHP_EOL;
            }
        }

        if (empty($append) && empty($prepend)) {
            $select = <<<HTML
<select name="$this->name" id="$id" $multiple class="form-control" $disabled $readonly >
    $option
</select>
HTML;
        } else {
            $select = <<<HTML
<div class="input-group">
    $prepend
    <select name="$this->name" id="$id" $multiple class="form-control" $disabled $readonly >
    $option
    </select>
    $append
</div>
HTML;

        }

        return <<<HTML

<div class="$class">
    <label for="$id">$label</label>
    $select
    $tips
</div>
HTML;
    }
}